home *** CD-ROM | disk | FTP | other *** search
/ Programmers Heaven 2 / Programmers Heaven 2.iso / files / graphics / library / wgt51_r2.zip / WGT5 / EXAMPLES / WGT70.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-03  |  6.2 KB  |  195 lines

  1. /*
  2. ==============================================================================
  3.                       WordUp Graphics Toolkit Version 5.0                     
  4.                              Demonstration Program 70
  5.                                                                               
  6.  Use a graphical display to calibrate the joystick.
  7.                                                                               
  8.  *** PROJECT ***                                                             
  9.  This program requires the file WGT5_WC.LIB to be linked.   
  10.                                                                               
  11.  *** DATA FILES ***                                                          
  12.  LITTLE.WFN
  13.                                                         WATCOM C++ VERSION 
  14. ==============================================================================
  15. */
  16.  
  17.  
  18. #include <stdlib.h>
  19. #include <conio.h>
  20. #include <wgt5.h>
  21. #include <wgtjoy.h>
  22.  
  23. void main (void)
  24. {
  25.   joystick joya;                        /* Structure for joystick A */
  26.   joystick joyb;                        /* Structure for joystick B */
  27.   int lastx_a, lastx_b;                 /* Previous joystick values */
  28.   int lasty_a, lasty_b;                 /* Previous joystick values */
  29.   int result;                           /* Result of joystick detection */
  30.   int joya_found = 0;                   /* 1 if joystick is found */
  31.   int joyb_found = 0;                   /* 1 if joystick is found */
  32.   wgtfont little;
  33.   short oldmode;  
  34.  
  35.   oldmode = wgetmode ();
  36.   if (!vgadetected ())
  37.   {
  38.     printf ("VGA is required to run this program...");
  39.     exit (1);
  40.   }
  41.  
  42.   printf ("WGT Example #70\n\n");
  43.   printf ("This program uses the joystick library to demonstrate the proper calibration\n");
  44.   printf ("techniques. Once the detected joysticks have been calibrated you may move the\n");
  45.   printf ("sticks around and press joystick buttons to see how the system reacts.\n");
  46.   printf ("Press any key to exit.\n");
  47.   printf ("\nPress any key to begin.\n");
  48.   getch ();
  49.  
  50.   little = wloadfont ("little.wfn");
  51.  
  52.   vga256 ();
  53.   wtextcolor (15);
  54.   wtexttransparent (TEXTFGBG);
  55.   
  56.   wouttextxy (0, 190, little, "Demonstrating joystick calibration system........");
  57.   wsetcolor (15);
  58.   wline (0, 188, 319, 188);
  59.   wsetcolor (2);
  60.   wbar (0, 0, 319, 187);
  61.   
  62.   wtextbackground (2);
  63.   if (!( result = wcheckjoystick () ))
  64.   {
  65.     wouttextxy (0, 0, NULL, "Joysticks not found. Program aborted.");
  66.     getch ();
  67.     wsetmode (3);
  68.     exit (0);
  69.   }
  70.  
  71.   if (result & 1)
  72.   {
  73.     joya_found = 1;                             /* It's there */
  74.     wouttextxy (0, 0, little, "Joystick A detected.");
  75.     winitjoystick (&joya, 0);                   /* Initialize it */
  76.     wouttextxy (0, 8, little, "Calibrate joystick A by swirling it and then pressing ENTER.");
  77.     wcalibratejoystick (&joya);
  78.     wouttextxy (0, 16, little, "Joystick A calibrated.");
  79.   }
  80.  
  81.   if (result & 2)
  82.   {
  83.     joyb_found = 1;                             /* It's there */
  84.     wouttextxy (0, 32, little, "Joystick B detected.");
  85.     winitjoystick (&joyb, 1);                   /* Initialize it */
  86.     wouttextxy (0, 40, little, "Calibrate joystick B by swirling it and then pressing ENTER.");
  87.     wcalibratejoystick (&joyb);
  88.     wouttextxy (0, 48, little, "Joystick B calibrated.");
  89.   }
  90.  
  91.   wbar (0, 0, 319, 187);
  92.   wouttextxy (0, 0, little, "Now reading joystick values. Press any key to end program.");
  93.  
  94.   joya.scale = 100;
  95.   joyb.scale = 100;
  96.   wsetcolor (15);
  97.   if (joya_found)
  98.   {
  99.     wtextcolor (6);
  100.     wouttextxy (0, 30, little, "Joystick A");
  101.     wouttextxy (0, 40, little, "X AXIS");
  102.     wouttextxy (0, 60, little, "Joystick A");
  103.     wouttextxy (0, 70, little, "Y AXIS");
  104.     wline (60, 40, 260, 40);
  105.     wline (160, 41, 160, 44);
  106.     wtextcolor (1);
  107.     wouttextxy (157, 46, little, "0");
  108.     wline (60, 41, 60, 44);
  109.     wouttextxy (48, 46, little, "-100");
  110.     wline (260, 41, 260, 44);
  111.     wouttextxy (253, 46, little, "100");
  112.   
  113.     wline (60, 70, 260, 70);
  114.     wline (160, 71, 160, 74);
  115.     wouttextxy (158, 76, little, "0");
  116.     wline (60, 71, 60, 74);
  117.     wouttextxy (47, 76, little, "-100");
  118.     wline (260, 71, 260, 74);
  119.     wouttextxy (253, 76, little, "100");
  120.   }
  121.   if (joyb_found)
  122.   {
  123.     wtextcolor (6);
  124.     wouttextxy (0, 130, little, "Joystick B");
  125.     wouttextxy (0, 140, little, "X AXIS");
  126.     wouttextxy (0, 160, little, "Joystick B");
  127.     wouttextxy (0, 170, little, "Y AXIS");
  128.     wline (60, 140, 260, 140);
  129.     wline (160, 141, 160, 144);
  130.     wtextcolor (1);
  131.     wouttextxy (157, 146, little, "0");
  132.     wline (60, 141, 60, 144);
  133.     wouttextxy (48, 146, little, "-100");
  134.     wline (260, 141, 260, 144);
  135.     wouttextxy (253, 146, little, "100");
  136.   
  137.     wline (60, 170, 260, 170);
  138.     wline (160, 171, 160, 174);
  139.     wouttextxy (158, 176, little, "0");
  140.     wline (60, 171, 60, 174);
  141.     wouttextxy (47, 176, little, "-100");
  142.     wline (260, 171, 260, 174);
  143.     wouttextxy (253, 176, little, "100");
  144.   }
  145.  
  146.   wtextcolor (15);
  147.   while (!kbhit ())
  148.   {
  149.     if (joya_found)
  150.     {
  151.       wreadjoystick (&joya);            /* Read values into structures */
  152.       if (joya.x != lastx_a)
  153.       {
  154.         wsetcolor (2);
  155.         wbar (60, 30, 260, 39);
  156.         wsetcolor (0);
  157.         wline (160 + joya.x, 30, 160 + joya.x, 39);
  158.         lastx_a = joya.x;
  159.       }
  160.       if (joya.y != lasty_a)
  161.       {
  162.         wsetcolor (2);
  163.         wbar (60, 60, 260, 69);
  164.         wsetcolor (0);
  165.         wline (160 + joya.y, 60, 160 + joya.y, 69);
  166.         lasty_a = joya.y;
  167.       }
  168.       wgtprintf (265, 60, little, "Buttons: %2d", joya.buttons & 0x03);
  169.     }
  170.     if (joyb_found)
  171.     {
  172.       wreadjoystick (&joyb);
  173.       if (joyb.x != lastx_b)
  174.       {
  175.         wsetcolor (2);
  176.         wbar (60, 130, 260, 139);
  177.         wsetcolor (0);
  178.         wline (160 + joyb.x, 130, 160 + joyb.x, 139);
  179.         lastx_b = joyb.x;
  180.       }
  181.       if (joyb.y != lasty_b)
  182.       {
  183.         wsetcolor (2);
  184.         wbar (60, 160, 260, 169);
  185.         wsetcolor (0);
  186.         wline (160 + joyb.y, 160, 160 + joyb.y, 169);
  187.         lasty_b = joyb.y;
  188.       }
  189.       wgtprintf (265, 160, little, "Buttons: %2d", (joyb.buttons & 0x0c) >> 2);
  190.     }
  191.   }
  192.   getch ();
  193.   wsetmode (3);
  194. }
  195.